package com.longzongqin.demo.controller;


import ch.qos.logback.core.util.FileUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.longzongqin.demo.entity.ImgInfo;
import com.longzongqin.demo.service.ImgInfoService;
import com.longzongqin.demo.service.UploaderService;
import com.longzongqin.demo.utils.JacksonUtil;
import com.longzongqin.demo.utils.UploaderUtil;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author longzongqin
 * @since 2018-03-19
 */
@Controller
@RequestMapping("/uploader")
public class UploaderController {

    @Autowired
    private UploaderService uploaderService;

    @GetMapping
    public String index(ModelMap map){
//        EntityWrapper<ImgInfo> ew = new EntityWrapper<ImgInfo>();
//        ew.eq("status",0);
//        ew.orderDesc(Arrays.asList("id"));
//        Page<ImgInfo> imgInfo = imgInfoService.selectPage(new Page<ImgInfo>(0,20),ew);
//        map.put("imgInfo", imgInfo);
        return "uploader/index";
    }

//    @PostMapping("upload")
//    @ResponseBody
//    public Boolean upload(MultipartFile file) {
//        File savefile = new File("/gdpHugeFileDIr",file.getOriginalFilename());
//        try {
//            file.transferTo(savefile);//将文件保存E盘下
//        } catch (IOException e) {
//            return false;
//        }
//        return true;
//    }


    /**
     * @author van
     * 检查文件存在与否
     */
    @PostMapping("checkFile")
    @ResponseBody
    public Boolean checkFile(@RequestParam(value = "md5File") String md5File) {
        return UploaderUtil.checkFile(md5File);
    }

    /**
     * @author van
     * 检查分片存在与否
     */
    @PostMapping("checkChunk")
    @ResponseBody
    public Boolean checkChunk(@RequestParam(value = "md5File") String md5File,
                              @RequestParam(value = "chunk") Integer chunk) {
        return UploaderUtil.checkChunk(md5File,chunk);
    }

    /**
     * @author van
     * 上传，这里根据文件md5值生成目录，并将分片文件放到该目录下
     */
    @PostMapping("upload")
    @ResponseBody
    public Boolean upload(@RequestParam(value = "file") MultipartFile file,
                          @RequestParam(value = "md5File") String md5File,
                          @RequestParam(value = "chunk",required= false) Integer chunk) { //第几片，从0开始
        return UploaderUtil.upload(file, md5File, chunk);
    }

    /**
     * @author van
     * 合成分片
     */
    @PostMapping("merge")
    @ResponseBody
    public Boolean merge(@RequestParam(value = "chunks",required =false) Integer chunks,
                          @RequestParam(value = "md5File") String md5File,
                          @RequestParam(value = "name") String name) throws Exception {
        uploaderService.merge(chunks, md5File, name);
        return true;
    }



}

